﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VSWClient.Tcp;
using VSWClient.Pages;
using VSWClient.Utility;
using System.Data;
using VSWClient.Tables;
using System.Diagnostics;
using VSWClient.Pages.Tables.L2PDB;
using VSWClient.Export;

namespace VSWClient
{
    /// <summary>
    /// MainWindow.xaml interaction logic
    /// </summary>
    public partial class MainWindow : Window
    {
        TcpClient client;
        ResourceDictionary dict;
        public MainWindow(TcpClient tcp, ResourceDictionary maindict)
        {
            InitializeComponent();
            client = tcp;
            dict = maindict;
            client.DealMessageHandler += Client_DealMessageHandler;
        }

        private void Client_DealMessageHandler(object sender, DealMessageHandlerArgs e)
        {
            try
            {
                for (int i = 0; i < e.Message.Length; i++)
                {
                    TcpMessageHelper msg = new TcpMessageHelper(e.Message[i]);
                    //whether command is valid
                    if (msg.Vaild)
                    {
                        switch (msg.CommandId)
                        {
                            #region General Handle
                            case Identity.initPageTask:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable[] tables = JsonHelper.DeserializeJsonToObject<DataTable[]>(msg.Parameter[0]);
                                        string generalTaskTotalPages = msg.Parameter[1];
                                        string taskTaskTotalPages = msg.Parameter[2];
                                        string recipeTotalPages = msg.Parameter[3];
                                        string batchSRTotalPages = msg.Parameter[4];
                                        string stripSRTotalPages = msg.Parameter[5];

                                        DataTable GeneralTaskTable = tables[0];
                                        DataTable taskTaskTable = tables[1];
                                        DataTable recipeTable = tables[2];
                                        DataTable batchSampleRuleTable = tables[3];
                                        DataTable stripSampleRuleTable = tables[4];
                                        DataTable proStandardTable = tables[5];
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            if (GeneralTaskTable != null)
                                            {
                                                GeneralTaskTable.TableName = TASK_INFO.generalTaskTableName;
                                                page_Task.RefreshPageTaskList(GeneralTaskTable);
                                                page_Task.textBox_GeneralTaskTotalPages.Text = generalTaskTotalPages;
                                                page_Task.textBox_GeneralTaskPage.Text = "1";
                                            }
                                            if (taskTaskTable != null)
                                            {
                                                taskTaskTable.TableName = TASK_INFO.taskTaskTableName;
                                                page_Task.RefreshPageTaskList(taskTaskTable);
                                                page_Task.textBox_taskTaskTotalPages.Text = taskTaskTotalPages;
                                                page_Task.textBox_taskTaskPage.Text = "1";
                                            }
                                            if (recipeTable != null)
                                            {
                                                recipeTable.TableName = RECIPE_INFO.tableName;
                                                page_Task.RefreshPageTaskList(recipeTable);
                                                page_Task.textBox_RecipeTotalPages.Text = recipeTotalPages;
                                                page_Task.textBox_RecipePage.Text = "1";
                                            }
                                            if (batchSampleRuleTable != null)
                                            {
                                                batchSampleRuleTable.TableName = BATCH_SAMPLE_RULE.tableName;
                                                page_Task.RefreshPageTaskList(batchSampleRuleTable);
                                                page_Task.textBox_BatchSampleRuleTotalPages.Text = batchSRTotalPages;
                                                page_Task.textBox_BatchSampleRulePage.Text = "1";
                                            }
                                            if (stripSampleRuleTable != null)
                                            {
                                                stripSampleRuleTable.TableName = STRIP_SAMPLE_RULE.tableName;
                                                page_Task.RefreshPageTaskList(stripSampleRuleTable);
                                                page_Task.textBox_StripSampleRuleTotalPages.Text = stripSRTotalPages;
                                                page_Task.textBox_StripSampleRulePage.Text = "1";
                                            }
                                            if (proStandardTable != null)
                                            {
                                                proStandardTable.TableName = PRODUCTION_STANDARD.tableName;
                                                page_Task.RefreshPageTaskList(proStandardTable);
                                            }
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.refreshGeneralTask:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string TotalPages = msg.Parameter[1];
                                        dataTable.TableName = TASK_INFO.generalTaskTableName;
                                        if (page_Task.taskTaskMode == "GeneralTaskDetail")
                                        {
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.textBox_GeneralTaskTotalPages.Text = TotalPages;
                                            }));
                                            if (dataTable.Rows.Count < page_Task.generalTask_Pagenum)
                                            {
                                                page_Task.generalTask_index_isChange = false;
                                            }
                                            else
                                            {
                                                page_Task.generalTask_index_isChange = true;
                                            }
                                        }
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        if (page_Task.taskTaskMode == "GeneralTaskDetail")
                                        {
                                            page_Task.generalTask_index_isChange = false;
                                            if (page_Task.generalTask_Index > 1)
                                            {
                                                page_Task.generalTask_Index--;
                                            }
                                        }
                                    }
                                    break;
                                }
                            case Identity.select_GeneralTaskByIndex:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string TotalPages = msg.Parameter[1];
                                        dataTable.TableName = TASK_INFO.generalTaskTableName;
                                        if (page_Task.taskTaskMode == "GeneralTaskDetail")
                                        {
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.textBox_GeneralTaskTotalPages.Text = TotalPages;
                                            }));
                                        }
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        if (page_Task.taskTaskMode == "GeneralTaskDetail")
                                        {
                                            if (page_Task.generalTask_Index > 1)
                                            {
                                                page_Task.generalTask_Index--;
                                            }
                                        }
                                    }
                                    break;
                                }
                            #endregion

                            #region Task Handle 
                            case Identity.refreshTaskTask:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string TotalPages = msg.Parameter[1];
                                        dataTable.TableName = TASK_INFO.taskTaskTableName;
                                        if (page_Task.taskTaskMode == "TaskTaskDetail")
                                        {
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.textBox_taskTaskTotalPages.Text = TotalPages;
                                            }));
                                            if (dataTable.Rows.Count < page_Task.taskTask_Pagenum)
                                            {
                                                page_Task.taskTask_index_isChange = false;
                                            }
                                            else
                                            {
                                                page_Task.taskTask_index_isChange = true;
                                            }
                                        }
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        if (page_Task.taskTaskMode == "TaskTaskDetail")
                                        {
                                            page_Task.taskTask_index_isChange = false;
                                            if (page_Task.taskTask_Index > 1)
                                            {
                                                page_Task.taskTask_Index--;
                                            }
                                        }
                                    }
                                    break;
                                }
                            case Identity.selectTaskByParameters:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable taskTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string TotalPages = msg.Parameter[1];
                                        taskTable.TableName = TASK_INFO.taskTaskTableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.textBox_taskTaskTotalPages.Text = TotalPages;
                                            page_Task.RefreshPageTaskList(taskTable);
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.insert_TASK:
                                {
                                    if (msg.Status)
                                    {
                                        try
                                        {
                                            DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                            dataTable.TableName = TASK_INFO.taskTaskTableName;
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.RefreshPageTaskList(dataTable);
                                            }));
                                        }
                                        catch
                                        {
                                            MessageBox.Show("Task insert sucess,refresh fail");
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("Task insert failed");
                                    }
                                    break;
                                }
                            case Identity.deleteUnloadingTask:
                                {
                                    if (msg.Status)
                                    {
                                        try
                                        {
                                            DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                            string taskTotalPages = msg.Parameter[1];
                                            dataTable.TableName = TASK_INFO.taskTaskTableName;
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.RefreshPageTaskList(dataTable);
                                                page_Task.textBox_taskTaskTotalPages.Text = taskTotalPages;
                                            }));
                                        }
                                        catch
                                        {
                                            MessageBox.Show("task delete sucessed,refresh fail");
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("task delete failed");
                                    }
                                    break;
                                }
                            case Identity.updateTaskByTaskNo:
                                {
                                    if (msg.Status)
                                    {
                                        try
                                        {
                                            DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                            dataTable.TableName = TASK_INFO.taskTaskTableName;
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.RefreshPageTaskList(dataTable);
                                                page_Task.Popup_Task.IsOpen = false;
                                            }));
                                        }
                                        catch
                                        {
                                            MessageBox.Show("Task edit sucess,refresh fail");
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("Task edit failed");
                                    }
                                    break;
                                }
                            #endregion
                            #region Strip Handle
                            case Identity.insert_STRIP_INFO:
                                {
                                    if (msg.Status)
                                    {
                                        try
                                        {
                                            DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                            string totalPages = msg.Parameter[1];
                                            dataTable.TableName = STRIP_INFO.popupTaskStripTableName;
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.RefreshPageTaskList(dataTable);
                                                page_Task.textBox_taskStripTotalPages.Text = totalPages;
                                                page_Task.textBox_TaskEditStripTotalPages.Text = totalPages;
                                            }));
                                        }
                                        catch
                                        {
                                            MessageBox.Show("Strip insert sucessed,refresh fail");
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("strip insert failed");
                                    }
                                    break;
                                }
                            case Identity.updateStripByStripNo:
                                {
                                    if (msg.Status)
                                    {
                                        try
                                        {
                                            DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                            dataTable.TableName = STRIP_INFO.popupTaskStripTableName;
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.RefreshPageTaskList(dataTable);
                                            }));
                                        }
                                        catch
                                        {
                                            MessageBox.Show("Strip edit sucessed,refresh fail");
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("strip edit failed");
                                    }
                                    break;
                                }
                            case Identity.deleteStripInfoByStrip_code:
                                {
                                    if (msg.Status)
                                    {
                                        try
                                        {
                                            DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                            string totalPages = msg.Parameter[1];
                                            dataTable.TableName = STRIP_INFO.popupTaskStripTableName;
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.RefreshPageTaskList(dataTable);
                                                page_Task.textBox_taskStripTotalPages.Text = totalPages;
                                            }));
                                        }
                                        catch
                                        {
                                            MessageBox.Show("strip delete sucessed,refresh fail");
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show("strip delete failed");
                                    }
                                    break;
                                }
                            case Identity.select_STRIP_INFOByTaskNo:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string stripTotalPages = msg.Parameter[1];
                                        dataTable.TableName = STRIP_INFO.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            if (page_Task.generalOrEditStrip == "generalStrip")
                                            {
                                                dataTable.TableName = STRIP_INFO.generalStripTableName;
                                                page_Task.textBox_GeneralStripTotalPages.Text = stripTotalPages;
                                                if (dataTable.Rows.Count < page_Task.generalStrip_Pagenum)
                                                {
                                                    page_Task.generalStrip_index_isChange = false;
                                                }
                                                else
                                                {
                                                    page_Task.generalStrip_index_isChange = true;
                                                }
                                                page_Task.RefreshPageTaskList(dataTable);
                                            }
                                            else if (page_Task.generalOrEditStrip == "taskStrip")
                                            {
                                                dataTable.TableName = STRIP_INFO.taskStripTableName;
                                                page_Task.textBox_taskStripTotalPages.Text = stripTotalPages;
                                                if (dataTable.Rows.Count < page_Task.taskStrip_Pagenum)
                                                {
                                                    page_Task.taskStrip_index_isChange = false;
                                                }
                                                else
                                                {
                                                    page_Task.taskStrip_index_isChange = true;
                                                }
                                                page_Task.RefreshPageTaskList(dataTable);
                                            }
                                            else if (page_Task.generalOrEditStrip == "popupTaskStrip")
                                            {
                                                dataTable.TableName = STRIP_INFO.popupTaskStripTableName;
                                                page_Task.textBox_TaskEditStripTotalPages.Text = stripTotalPages;
                                                if (dataTable.Rows.Count < page_Task.taskEditStrip_Pagenum)
                                                {
                                                    page_Task.taskEditStrip_index_isChange = false;
                                                }
                                                else
                                                {
                                                    page_Task.taskEditStrip_index_isChange = true;
                                                }
                                                page_Task.RefreshPageTaskList(dataTable);
                                            }

                                        }));
                                    }
                                    else
                                    {
                                        if (page_Task.taskTaskMode == "generalStrip")
                                        {
                                            page_Task.generalStrip_index_isChange = false;
                                            if (page_Task.generalStrip_Index > 1)
                                            {
                                                page_Task.generalStrip_Index--;
                                            }
                                        }
                                        else if (page_Task.taskTaskMode == "taskStrip")
                                        {
                                            page_Task.taskStrip_index_isChange = false;
                                            if (page_Task.taskStrip_Index > 1)
                                            {
                                                page_Task.taskStrip_Index--;
                                            }
                                        }
                                    }
                                    break;
                                }
                            #endregion
                            #region Recipe Handle
                            case Identity.insert_Recipe:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = RECIPE_INFO.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("recipe add failed");
                                    }
                                    break;
                                }
                            case Identity.edit_Recipe:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = RECIPE_INFO.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("recipe edit failed");
                                    }
                                    break;
                                }
                            case Identity.deleteRecipeByRecipe_id:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string recipeTotalPages = msg.Parameter[1];
                                        dataTable.TableName = RECIPE_INFO.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.textBox_RecipeTotalPages.Text = recipeTotalPages;
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("recipe delete failed");
                                    }
                                    break;
                                }
                            case Identity.select_RecipeByIndex:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string recipeTotalPages = msg.Parameter[1];
                                        dataTable.TableName = RECIPE_INFO.tableName;
                                        if (page_Task.recipeDetailMode == "RecipeDetail")
                                        {
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.textBox_RecipeTotalPages.Text = recipeTotalPages;
                                            }));

                                        }
                                        else if (page_Task.recipeDetailMode == "RecipeBind")
                                        {
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.textBox_BindRecipeTotalPages.Text = recipeTotalPages;
                                            }));

                                        }
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        if (page_Task.recipeDetailMode == "RecipeDetail")
                                        {
                                            page_Task.recipeDetail_index_isChange = false;
                                            if (page_Task.recipeDetail_Index > 1)
                                            {
                                                page_Task.recipeDetail_Index--;
                                            }
                                        }
                                        else if (page_Task.recipeDetailMode == "RecipeBind")
                                        {
                                            page_Task.recipeBind_index_isChange = false;
                                            if (page_Task.recipeBind_Index > 1)
                                            {
                                                page_Task.recipeBind_Index--;
                                            }
                                        }
                                    }
                                    break;
                                }
                            case Identity.selectRecipeByParameters:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable recipeTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string TotalPages = msg.Parameter[1];
                                        recipeTable.TableName = RECIPE_INFO.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            if (page_Task.recipeDetailMode == "RecipeBind")
                                            {
                                                page_Task.textBox_BindRecipeTotalPages.Text = TotalPages;
                                            }
                                            else if (page_Task.recipeDetailMode == "RecipeDetail")
                                            {
                                                page_Task.textBox_RecipeTotalPages.Text = TotalPages;
                                            }
                                            page_Task.RefreshPageTaskList(recipeTable);
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            #endregion
                            #region BatchSampleRule Handle
                            case Identity.insert_BatchSampleRule:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = BATCH_SAMPLE_RULE.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("batch sample rule add failed");
                                    }
                                    break;
                                }
                            case Identity.edit_BatchSampleRule:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = BATCH_SAMPLE_RULE.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("batch sample rule edit failed");
                                    }
                                    break;
                                }
                            case Identity.delete_BatchSampleRule:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = BATCH_SAMPLE_RULE.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("batch sample rule delete failed");
                                    }
                                    break;
                                }
                            case Identity.select_BatchSampleRuleByIndex:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string batchSampleRuleTotalPages = msg.Parameter[1];
                                        dataTable.TableName = BATCH_SAMPLE_RULE.tableName;
                                        if (page_Task.batchSampleRuleDetailMode == "BatchSampleRuleDetail")
                                        {
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.textBox_BatchSampleRuleTotalPages.Text = batchSampleRuleTotalPages;
                                            }));
                                            if (dataTable.Rows.Count < page_Task.batchSampleRuleDetail_Pagenum)
                                            {
                                                page_Task.batchSampleRuleDetail_index_isChange = false;
                                            }
                                            else
                                            {
                                                page_Task.batchSampleRuleDetail_index_isChange = true;
                                            }
                                        }
                                        else if (page_Task.batchSampleRuleDetailMode == "BatchSampleRuleBind")
                                        {
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.textBox_BindBatchSampleRuleTotalPages.Text = batchSampleRuleTotalPages;
                                            }));
                                            if (dataTable.Rows.Count < page_Task.batchSampleRuleBind_Pagenum)
                                            {
                                                page_Task.batchSampleRuleBind_index_isChange = false;
                                            }
                                            else
                                            {
                                                page_Task.batchSampleRuleBind_index_isChange = true;
                                            }
                                        }

                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        if (page_Task.batchSampleRuleDetailMode == "BatchSampleRuleDetail")
                                        {
                                            page_Task.batchSampleRuleDetail_index_isChange = false;
                                            if (page_Task.batchSampleRuleDetail_Index > 1)
                                            {
                                                page_Task.batchSampleRuleDetail_Index--;
                                            }
                                        }
                                        else if (page_Task.batchSampleRuleDetailMode == "BatchSampleRuleBind")
                                        {
                                            page_Task.batchSampleRuleBind_index_isChange = false;
                                            if (page_Task.batchSampleRuleBind_Index > 1)
                                            {
                                                page_Task.batchSampleRuleBind_Index--;
                                            }
                                        }
                                    }
                                    break;
                                }
                            #endregion
                            #region StripSampleRule Handle
                            case Identity.insert_StripSampleRule:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = STRIP_SAMPLE_RULE.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("strip sample rule add failed");
                                    }
                                    break;
                                }
                            case Identity.edit_StripSampleRule:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = STRIP_SAMPLE_RULE.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("strip sample rule edit failed");
                                    }
                                    break;
                                }
                            case Identity.delete_StripSampleRule:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = STRIP_SAMPLE_RULE.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("strip sample rule delete failed");
                                    }
                                    break;
                                }
                            case Identity.select_StripSampleRuleByIndex:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string stripSampleRuleTotalPages = msg.Parameter[1];
                                        dataTable.TableName = STRIP_SAMPLE_RULE.tableName;
                                        if (page_Task.stripSampleRuleDetailMode == "StripSampleRuleDetail")
                                        {
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.textBox_StripSampleRuleTotalPages.Text = stripSampleRuleTotalPages;
                                            }));
                                            if (dataTable.Rows.Count < page_Task.stripSampleRuleDetail_Pagenum)
                                            {
                                                page_Task.stripSampleRuleDetail_index_isChange = false;
                                            }
                                            else
                                            {
                                                page_Task.stripSampleRuleDetail_index_isChange = true;
                                            }
                                        }
                                        else if (page_Task.stripSampleRuleDetailMode == "StripSampleRuleBind")
                                        {
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Task.textBox_BindStripSampleRuleTotalPages.Text = stripSampleRuleTotalPages;
                                            }));
                                            if (dataTable.Rows.Count < page_Task.stripSampleRuleBind_Pagenum)
                                            {
                                                page_Task.stripSampleRuleBind_index_isChange = false;
                                            }
                                            else
                                            {
                                                page_Task.stripSampleRuleBind_index_isChange = true;
                                            }
                                        }

                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        if (page_Task.stripSampleRuleDetailMode == "StripSampleRuleDetail")
                                        {
                                            page_Task.stripSampleRuleDetail_index_isChange = false;
                                            if (page_Task.stripSampleRuleDetail_Index > 1)
                                            {
                                                page_Task.stripSampleRuleDetail_Index--;
                                            }
                                        }
                                        else if (page_Task.stripSampleRuleDetailMode == "StripSampleRuleBind")
                                        {
                                            page_Task.stripSampleRuleBind_index_isChange = false;
                                            if (page_Task.stripSampleRuleBind_Index > 1)
                                            {
                                                page_Task.stripSampleRuleBind_Index--;
                                            }
                                        }
                                    }
                                    break;
                                }
                            #endregion
                            #region ProductionStandard Handle
                            case Identity.selectProductionStandard:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = PRODUCTION_STANDARD.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    break;
                                }
                            case Identity.insert_ProductionStandard:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = PRODUCTION_STANDARD.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("insert_ProductionStandard failed");
                                    }
                                    break;
                                }
                            case Identity.edit_ProductionStandard:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = PRODUCTION_STANDARD.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Task.RefreshPageTaskList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("edit_ProductionStandard failed");
                                    }
                                    break;
                                }

                            #endregion


                            #region DownTime Handle
                            case Identity.innitPageDownTime:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        dataTable.TableName = DOWNTIME_TABLE.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Downtime.RefreshPageDownTimeList(dataTable);
                                            page_Downtime.textBox_DownTimeTotalPages.Text = totalPages;
                                            TEAM_TABLE.teamtable.TableName = TEAM_TABLE.tableName;
                                            page_Downtime.RefreshPageDownTimeList(TEAM_TABLE.teamtable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.updatedowntime:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        dataTable.TableName = DOWNTIME_TABLE.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Downtime.RefreshPageDownTimeList(dataTable);
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("Down time update failed");
                                    }
                                    break;
                                }
                            case Identity.selectV7_DOWNTIME:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        dataTable.TableName = DOWNTIME_TABLE.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Downtime.RefreshPageDownTimeList(dataTable);
                                            page_Downtime.textBox_DownTimeTotalPages.Text = totalPages;
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show("Down time query failed");
                                    }
                                    break;
                                }
                            case Identity.adddowntime:
                                {
                                    try
                                    {
                                        if (msg.Status)
                                        {
                                            DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                            dataTable.TableName = DOWNTIME_TABLE.tableName;
                                            Dispatcher.Invoke(new Action(() =>
                                            {
                                                page_Downtime.RefreshPageDownTimeList(dataTable);
                                            }));
                                        }
                                        else
                                        {
                                            LogHelper.WriteLog("Client_DealMessageHandler.adddowntime fail");
                                        }
                                        break;
                                    }
                                    catch (Exception ex)
                                    {
                                        LogHelper.WriteLog("Client_DealMessageHandler.adddowntime", ex);
                                        throw ex;
                                    }

                                }
                            #endregion

                            #region Query Batch 
                            case Identity.queryBatchByBatchNo:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable[] tables = JsonHelper.DeserializeJsonToObject<DataTable[]>(msg.Parameter[0]);
                                        string stripTotalPages = msg.Parameter[1];
                                        string pipeTotalPages = msg.Parameter[2];
                                        Dictionary<string, double> dic = JsonHelper.DeserializeJsonToObject<Dictionary<string, double>>(msg.Parameter[3]);
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Query.RefreshPageQueryBatch(tables, stripTotalPages, pipeTotalPages, dic);
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.queryBatchStripByBatchNo:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        dataTable.TableName = STRIP_INFO.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Query.RefreshPageQueryBatchList(dataTable);
                                            page_Query.textBox_QueryBatchStripTotalPages.Text = totalPages;
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.queryBatchPipeByBatchNo:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable dataTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        dataTable.TableName = PIPE_INFO.tableName;
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Query.RefreshPageQueryBatchList(dataTable);
                                            page_Query.textBox_QueryBatchPipeTotalPages.Text = totalPages;
                                        }));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            #endregion
                            #region Query Strip 
                            case Identity.queryStripStripByStripNo:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable stripTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            stripTable.TableName = STRIP_INFO.tableName;
                                            page_Query.RefreshPageQueryStripList(stripTable);
                                            page_Query.textBox_QueryStripStripTotalPages.Text = totalPages;
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.queryStripPipeByStripNo:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable table = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            table.TableName = PIPE_INFO.tableName;
                                            page_Query.RefreshPageQueryStripList(table);
                                            page_Query.textBox_StripPipeTotalPages.Text = totalPages;
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }

                            #endregion
                            #region Query Pipe
                            case Identity.queryPipePipeByPipeNo:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable table = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            table.TableName = PIPE_INFO.tableName;
                                            page_Query.RefreshPageQueryPipeList(table);
                                            page_Query.textBox_QueryPipeTotalPages.Text = totalPages;
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.queryPipePipePassportAndTaskByPipeNo:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable table = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        Dictionary<string, string> dic = JsonHelper.DeserializeJsonToObject<Dictionary<string, string>>(msg.Parameter[1]);
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Query.RefreshPageQueryPipePassportList(table, dic);
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.queryPipeExportPipeProcessData:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable[] tables = JsonHelper.DeserializeJsonToObject<DataTable[]>(msg.Parameter[0]);
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            if (tables != null || tables.Length == 7)
                                            {
                                                string[] str = { "StripHfw", "StripLht1", "StripLht2", "StripLht3", "StripLht4", "StripSQ", "StripSUST" };
                                                List<string> lists = new List<string>(str);
                                                NPOIToExcel.ExportToExcel(tables, System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/xls/Pipe_ProcessData.xls", lists);
                                            }
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.query_PIPE_PARAMETER:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable pipeTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            pipeTable.TableName = PIPE_INFO.tableName;
                                            page_Query.RefreshPageQueryList(pipeTable);
                                            //page_Query.textBox_PipeTotalPages.Text = totalPages;
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            #endregion
                            #region Query Bundle 
                            case Identity.queryBundleByBundleNo:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable table = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            table.TableName = BUNDLE_TABLE.tableName;
                                            page_Query.RefreshPageQueryBundleList(table);
                                            page_Query.textBox_QueryBundleTotalPages.Text = totalPages;
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            case Identity.queryBundlePipeByBundleNo:
                                {
                                    if (msg.Status)
                                    {
                                        Dictionary<string, DataTable> dicBatchPipe = JsonHelper.DeserializeJsonToObject<Dictionary<string, DataTable>>(msg.Parameter[0]);
                                        Dictionary<string, string> dicBundlePipe = JsonHelper.DeserializeJsonToObject<Dictionary<string, string>>(msg.Parameter[1]);
                                        DataTable dicBundleTotal = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[2]);
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            page_Query.RefreshPageQueryBundlePipeList(dicBundlePipe, dicBatchPipe, dicBundleTotal);
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            #endregion
                            #region Query RecipeLog
                            case Identity.queryRecipeLog:
                                {
                                    if (msg.Status)
                                    {
                                        DataTable recipelogTable = JsonHelper.DeserializeJsonToObject<DataTable>(msg.Parameter[0]);
                                        string totalPages = msg.Parameter[1];
                                        Dispatcher.Invoke(new Action(() =>
                                        {
                                            recipelogTable.TableName = RECIPE_EVENT.tableName;
                                            page_Query.RefreshPageQueryList(recipelogTable);
                                            page_Query.textBox_RecipeLogTotalPages.Text = totalPages;
                                        }
                                         ));
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg.Parameter[0]);
                                    }
                                    break;
                                }
                            #endregion

                            default:
                                {
                                    break;
                                }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Command is not valid");
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog("Client_DealMessageHandler", ex);
                throw ex;
            }
        }

        Page_Cutting page_Cutting;
        Page_Task page_Task;
        Page_FN page_FN;
        Page_Downtime page_Downtime;
        Page_Query page_Query;
        Page_Recipe page_Recipe;
        Page_Report page_Report;
        Page_User page_User;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            TextBlock_Operator.Text = USER_TABLE.user_Name;
            textBlock_Shift.Text = TEAM_TABLE.CurrentShiftName;

            page_Task = new Page_Task(client);
            page_Task.Resources.MergedDictionaries.Clear();
            page_Task.Resources.MergedDictionaries.Add(dict);

            page_FN = new Page_FN(client);
            page_FN.Resources.MergedDictionaries.Clear();
            page_FN.Resources.MergedDictionaries.Add(dict);

            page_Downtime = new Page_Downtime(client);
            page_Downtime.Resources.MergedDictionaries.Clear();
            page_Downtime.Resources.MergedDictionaries.Add(dict);

            page_Cutting = new Page_Cutting(client);
            page_Cutting.Resources.MergedDictionaries.Clear();
            page_Cutting.Resources.MergedDictionaries.Add(dict);

            page_Query = new Page_Query(client);
            page_Query.Resources.MergedDictionaries.Clear();
            page_Query.Resources.MergedDictionaries.Add(dict);

            page_Recipe = new Page_Recipe(client);
            page_Recipe.Resources.MergedDictionaries.Clear();
            page_Recipe.Resources.MergedDictionaries.Add(dict);

            page_Report = new Page_Report(client);
            page_Report.Resources.MergedDictionaries.Clear();
            page_Report.Resources.MergedDictionaries.Add(dict);

            page_User = new Page_User(client);
            page_Report.Resources.MergedDictionaries.Clear();
            page_Report.Resources.MergedDictionaries.Add(dict);

            frame.Navigate(page_Task);
            //DispatcherTimer dispatcherTimer = new DispatcherTimer();
            //dispatcherTimer.Interval = TimeSpan.FromMilliseconds(10000);
            //dispatcherTimer.Tick += DispatcherTimer_Tick; 
            //dispatcherTimer.Start();
        }

        #region  window zoom in and out
        private void Btn_Hide_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void Btn_ZoonIn_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Maximized;
            btn_ZoonIn.Visibility = Visibility.Collapsed;
            btn_ZoonOut.Visibility = Visibility.Visible;
        }

        private void Btn_ZoonOut_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Normal;
            btn_ZoonOut.Visibility = Visibility.Collapsed;
            btn_ZoonIn.Visibility = Visibility.Visible;
        }

        private void Btn_Drag_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.WindowState == WindowState.Normal)
            {
                this.WindowState = WindowState.Maximized;
                btn_ZoonIn.Visibility = Visibility.Collapsed;
                btn_ZoonOut.Visibility = Visibility.Visible;
            }
            else
            {
                this.WindowState = WindowState.Normal;
                btn_ZoonOut.Visibility = Visibility.Collapsed;
                btn_ZoonIn.Visibility = Visibility.Visible;
            }
        }
        #endregion


        //private void Btn_Drag_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    if (this.WindowState == WindowState.Normal)
        //    {
        //        this.WindowState = WindowState.Maximized;
        //        btn_ZoonIn.Visibility = Visibility.Collapsed;
        //        btn_ZoonOut.Visibility = Visibility.Visible;
        //    }
        //    else
        //    {
        //        this.WindowState = WindowState.Normal;
        //        btn_ZoonOut.Visibility = Visibility.Collapsed;
        //        btn_ZoonIn.Visibility = Visibility.Visible;
        //    }
        //}

        /// <summary>
        /// window main menu button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShiftPage_Click(object sender, RoutedEventArgs e)
        {
            Button button = e.Source as Button;
            switch (button.Name)
            {
                case "btn_task":
                    {
                        if (Privilege.IsPrivilege(USER_TABLE.user_Level, btn_task.Tag.ToString()))
                        {
                            if (frame.Content != page_Task)
                                frame.Navigate(page_Task);
                        }
                        else
                        {
                            MessageBox.Show("You do not have the permission");
                        }

                        break;
                    }
                case "btn_downtime":
                    {
                        if (Privilege.IsPrivilege(USER_TABLE.user_Level, btn_downtime.Tag.ToString()))
                        {
                            if (frame.Content != page_Downtime)
                                frame.Navigate(page_Downtime);
                        }
                        else
                        {
                            MessageBox.Show("You do not have the permission");
                        }
                        break;
                    }


                case "btn_query":
                    {
                        if (Privilege.IsPrivilege(USER_TABLE.user_Level, btn_query.Tag.ToString()))
                        {
                            if (frame.Content != page_Query)
                                frame.Navigate(page_Query);
                        }
                        else
                        {
                            MessageBox.Show("You do not have the permission");
                        }
                        break;
                    }

                case "btn_report":
                    {
                        if (Privilege.IsPrivilege(USER_TABLE.user_Level, btn_report.Tag.ToString()))
                        {
                            if (frame.Content != page_Report)
                                frame.Navigate(page_Report);
                        }
                        else
                        {
                            MessageBox.Show("You do not have the permission");
                        }
                        break;
                    }
                case "btn_users":
                    {
                        if (Privilege.IsPrivilege(USER_TABLE.user_Level, btn_users.Tag.ToString()))
                        {
                            if (frame.Content != page_User)
                                frame.Navigate(page_User);
                        }
                        else
                        {
                            MessageBox.Show("You do not have the permission");
                        }
                        break;
                    }

            }
        }

        #region Modify Password
        /// <summary>
        /// modify user passord 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_UserPasswordModify_Click(object sender, RoutedEventArgs e)
        {
            popup_Edit_Password.IsOpen = true;
            textBox_Edit_UserName.Text = USER_TABLE.user_Name;
            textBox_Edit_UserID.Text = USER_TABLE.user_No;
        }
        /// <summary>
        /// save user password
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Save_Password_Click(object sender, RoutedEventArgs e)
        {
            if (textBox_Edit_OldPassword.Password != USER_TABLE.user_Password)
            {
                MessageBox.Show("User password error");
                return;
            }
            if (textBox_Edit_NewPassword.Password != textBox_Edit_ConfirmPassword.Password)
            {
                MessageBox.Show("Inconsistent passwords.");
                return;
            }
            client.SendMessage(Identity.definedCommand, Identity.update_USERPASSWORD, textBox_Edit_UserID.Text.ToString() + ";" + textBox_Edit_NewPassword.Password.ToString());
        }
        /// <summary>
        /// close popup modify password
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_popup_close_Click(object sender, RoutedEventArgs e)
        {
            popup_Edit_Password.IsOpen = false;
        }
        /// <summary>
        /// close main window
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_Close_Click(object sender, RoutedEventArgs e)
        {
            client.Disconnect();
            this.Close();
        }

        #endregion

        #region Popup_Edit_Password Dragable
        bool isDownPopup_Edit_Password;
        Point originalPointPopup_Edit_Password;

        public Point OriginalPointPopup_Edit_Password { get => originalPointPopup_Edit_Password; set => originalPointPopup_Edit_Password = value; }
        public bool IsDownPopup_Edit_Password { get => isDownPopup_Edit_Password; set => isDownPopup_Edit_Password = value; }
        private void btn_Popup_Edit_Password_ChangePosition_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IsDownPopup_Edit_Password = true;
            OriginalPointPopup_Edit_Password = e.GetPosition(popup_Edit_Password);
        }

        private void btn_Popup_Edit_Password_ChangePosition_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsDownPopup_Edit_Password = false;
        }

        private void btn_Popup_Edit_Password_ChangePosition_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (IsDownPopup_Edit_Password)
            {
                Point pos = e.GetPosition(popup_Edit_Password);
                popup_Edit_Password.HorizontalOffset += pos.X - OriginalPointPopup_Edit_Password.X;
                popup_Edit_Password.VerticalOffset += pos.Y - OriginalPointPopup_Edit_Password.Y;
                OriginalPointPopup_Edit_Password = pos;
            }
        }
        #endregion

        #region Kill Process
        private void Window_Closed(object sender, EventArgs e)
        {
            try
            {
                Process[] p = Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName.Replace(".exe", ""));
                p[0].Kill();
            }
            catch { }
            finally
            {
                Application.Current.Shutdown();
            }
        }
        #endregion
    }
}
